package com.botarena.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.botarena.client.contest.remote.ContestAccess;
import com.botarena.client.contest.remote.ContestAccessAsync;
import com.botarena.shared.ContestInfo;
import com.botarena.shared.ContestState;
import com.botarena.shared.UserInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RightMenu extends VerticalPanel {

	private ListBox contestList;
	private Panel battlePanel;

	private Map<String, ContestInfo> availableContests = new HashMap<String, ContestInfo>();

	RightMenu(UserInfo userInfo) {
		if (userInfo.hasAccount()) {
			add(createUserPanel(userInfo));
		}
		
		battlePanel = new VerticalPanel();
		
		add(createContestPanel());
		add(battlePanel);
	}

	public ContestInfo getSelectedContest() {
		int index = contestList.getSelectedIndex();
		if(index == -1)
			return null;
		String item = contestList.getItemText(index);
		return availableContests.get(item);
	}

	private Panel createUserPanel(UserInfo userInfo) {
		VerticalPanel panel = new VerticalPanel();
		panel.setStyleName("box");

		panel.add(new Label("User: " + userInfo.getName()));
		if (userInfo.hasAvatar()) {
			Image avatar = new Image(userInfo.getAvatarUrl());
			avatar.setStyleName("avatar");
			panel.add(avatar);
		} else {
			Anchor avatarLink = new Anchor("add avatar...", SiteTokens
					.hashToken(SiteTokens.ACCOUNT));
			panel.add(avatarLink);
		}

		return panel;
	}

	private Panel createContestPanel() {
		VerticalPanel contestSelector = new VerticalPanel();
		contestSelector.setStyleName("box");

		contestList = new ListBox();
		
		ContestAccessAsync daa = GWT.create(ContestAccess.class);
		daa.getAllContests(new AsyncCallback<List<ContestInfo>>() {

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(List<ContestInfo> result) {
				System.out.println("inside " + result.size());
				for (ContestInfo contest : result) {
					availableContests.put(contest.getName(), contest);
					contestList.addItem(contest.getName());
				}
				
				if (contestList.getItemCount() > 0) {
					contestList.setSelectedIndex(0);
					battlePanel.add(createBattleInfoPanel());
					ContentManager.reloadContent();
				}
			}

		});

		contestList.addChangeHandler(new ChangeHandler(){

			@Override
			public void onChange(ChangeEvent event) {
				battlePanel.clear();
				battlePanel.add(createBattleInfoPanel());
				ContentManager.reloadContent();
			}
			
		});
		
		
		Label contestLab = new Label("Select contest: ");
		contestLab.setStyleName("title2");
		
		contestSelector.add(contestLab);
		contestSelector.add(contestList);

		return contestSelector;
	}

	private Panel createBattleInfoPanel() {

		VerticalPanel battleInfo = new VerticalPanel();
		battleInfo.setStyleName("box");

		if(contestList.getSelectedIndex() == -1)
			return battleInfo;
		
		Panel nextFight = new VerticalPanel();
		nextFight.add(new Label("Next fight in: "));
		nextFight.add(new Label("00:00:00")); // TODO: get data from
		// BattleService
		
		ContestInfo contest = getSelectedContest();

		final VerticalPanel contestants = new VerticalPanel();
		final VerticalPanel bots = new VerticalPanel();

		contestants.add(UIHelper.createSmallLoader(""));
		bots.add(UIHelper.createSmallLoader(""));

		ContestAccessAsync daa = GWT.create(ContestAccess.class);
		daa.getContestState(contest.getKey(), new AsyncCallback<ContestState>() {

			@Override
			public void onFailure(Throwable caught) {
				clear();
				contestants.add(new Label("Users: ?"));
				bots.add(new Label("Bots: ?"));
			}

			@Override
			public void onSuccess(ContestState result) {
				clear();
				contestants.add(new Label("Users: " + result.usersCount));
				bots.add(new Label("Bots: " + result.botsCount));
			}

			private void clear() {
				contestants.clear();
				bots.clear();
			}

		});

		battleInfo.add(nextFight);
		battleInfo.add(contestants);
		battleInfo.add(bots);

		return battleInfo;
	}

}
